package com.springmvc.springbootOne.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

@Controller
public class UploadController {
    
    @RequestMapping(value = "/upload",method = RequestMethod.GET)
	//@GetMapping(value="/index2")
    public String toIndex(){
		System.out.println("uuuuuufdfd");
    	 return "/upload";  
    }
    
	@RequestMapping(value ="/ntkoupload2",method=RequestMethod.POST)
	public void ntkofmanupload(HttpServletRequest request) throws IOException{
		System.out.println("ntkoupload2 is coming...");
		CommonsMultipartResolver comm=new  CommonsMultipartResolver(request.getSession().getServletContext());
		comm.setDefaultEncoding("utf-8");
		if(comm.isMultipart(request)){		
			MultipartHttpServletRequest mult=comm.resolveMultipart(request);	
			System.out.println("mult.getMultiFileMap().size()="+mult.getMultiFileMap().size());
			Map map= mult.getFileMap();
			System.out.println("map:"+map);
			Set set = map.entrySet();
			System.out.println("set:"+set);
			Iterator iterator= set.iterator();
			while(iterator.hasNext()){				
				Entry entry=(Entry) iterator.next();
				String key=(String) entry.getKey();
				List<MultipartFile> multipartFile= mult.getFiles(key);
				int size=multipartFile.size();
				String path=request.getSession().getServletContext().getRealPath("/")+"ntkofmanwenjian\\";
				for(int i=0;i<size;i++){
					String filename=multipartFile.get(i).getOriginalFilename();
					File ntkofile=new File(path+filename);
					FileOutputStream ntkoout=new FileOutputStream(ntkofile);
					byte[] by= multipartFile.get(i).getBytes();
					ntkoout.write(by);
					ntkoout.close();
				}
				
			}
			iterator.remove();
		}
	}
	
    @RequestMapping(value="/ntkoupload3", method = RequestMethod.POST)
    public @ResponseBody String multifileUpload(HttpServletRequest request){
    	System.out.println("upload is coming......");
        List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("ntkofman");
        MultipartFile file = null;
        BufferedOutputStream stream = null;
        System.out.println("files.size()=="+files.size());
        for (int i = 0; i < files.size(); ++i) {
            file = files.get(i);
            if (!file.isEmpty()) {
            	String uploadFilePath = file.getOriginalFilename();
            	System.out.println("file="+uploadFilePath);
                try {
                    byte[] bytes = file.getBytes();
                    stream = new BufferedOutputStream(new FileOutputStream(new File(".//uploadFiles//"+file.getOriginalFilename()) ));
                    stream.write(bytes);
                    stream.close();
                } catch (Exception e) {
                    stream = null;
                    return "You failed to upload " + i + " => " + e.getMessage();
                }
            } else {
                return "You failed to upload " + i + " because the file was empty.";
            }
        }
        return "upload successful";
    }
}
